<?php session_start() ?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>Wishlist</title>
</head>
<body>
<?php
require_once 'header.php';
?>
<div id="wishlist-body">
	<div>
		<h1 id="wishlist-heading">Wishlist</h1>
	</div>


<?php
require 'connect.php';

if (isset($_SESSION['user_id'])){

	$current_user_id = $_SESSION['user_id'];
	$sql = "SELECT property_id  FROM wishlists WHERE user_id = ".$current_user_id;

	$result_wishlist = mysqli_query($conn, $sql);

//		echo print_r ($result_wishlist);		///displaying for debuging purposes only

	if (mysqli_num_rows($result_wishlist) > 0){

		while($row = mysqli_fetch_assoc($result_wishlist)){

		//echo print_r($row),"<br>";			///displaying for debuging purposes only 

 			$sql = "SELECT bedrooms, price, description, suburb, city, street_name, street_number, properties.id FROM properties 
					INNER JOIN addresses ON addresses.property_id = properties.id
					WHERE properties.id = ".$row['property_id'];

			$result = mysqli_query($conn, $sql);

//		echo print_r ($result);				///displaying for debuging purposes only 

			if (mysqli_num_rows($result) > 0){

				$property_data = mysqli_fetch_assoc($result);

	
					$wishlist_state = "Remove from Wishlist";
					$btn_color = "add-to-wishlist-btn-toggle add-to-wishlist-btn";
		

				//getting photos urls or current property
				$sql = "SELECT photo_url FROM photos WHERE photos.property_id = ".$property_data['id'];
				$result_photos = mysqli_query($conn, $sql);
				if (mysqli_num_rows($result_photos) > 0){ ?>

					<div>
					<?php $current_property_photo_urls = mysqli_fetch_assoc($result_photos);
					//echo ($current_property_photo_urls['photo_url']);////displaying for debuging purposes only
					$photoUrl = './'.$current_property_photo_urls['photo_url'];
					?>
				</div>
				<section class="search-results-body">
					<div class="column one-half">
				        <div class="col-group dis-card">
				          <div class="img-container">
				            <img src="<?php echo $photoUrl;?>" class="house-img" width="300px" height="200px">
				          </div>
				          <h2 class="house-title"><?php echo $property_data['street_number']. ' '.$property_data['street_name']. ', '.$property_data['suburb']. ', '.$property_data['city']  ?></h2>
				          <div class="bedroom-wrapper">
						  	<span class="house-price"><?php echo '$'.number_format($property_data['price']); ?></span>
				            <span class="bed-number"><?php echo $property_data['bedrooms'] ?></span>
				            <img src="WebsiteImg/bed.png" class="bed-img">
				          </div>
						  <p class="desc-text-cards"><?php echo $property_data['description'] ?></p>
						  <a class="<?php echo $btn_color;?> addtowishlist whishstate" onclick="AddWhish(<?php echo $property_data['id']; ?>)" data="<?php echo $property_data['id']; ?>"><?php echo $wishlist_state;?></a>
				        </div>
				      </div>
				</section>


<?php
				}
			}			
		}
	}
	else { header('location: search.php'); }
}
else { header('location: login.php'); }
				
?>

<script type="text/javascript">
function AddWhish(id){
	var link_data = id;
	   // console.log('Link_data:'+link_data);
       $.ajax({
          type: "POST",
          url: 'addtowishlist.php',
          cache:false,
          data: { link_data: link_data },
          	success:function(result){
				  // console.log("AJ success"+result);
               if(result == 1)
               {
				   // alert ('pizdit');
                   $('a[data="' + link_data + '"]').addClass("add-to-wishlist-btn-toggle").text("Remove from Wishlist");
               }
               else{
				   // alert ('ne pizdit');
                   $('a[data="' + link_data + '"]').removeClass("add-to-wishlist-btn-toggle").text("Add to Wishlist");
               }
          },
		  error:function(result){
			// console.log("AJ error");

		  }
       });   
}
</script>
	
</div>
</body>
</html>
